package com.jiangnan.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.jiangnan.entity.PageResult
import com.jiangnan.entity.Res
import com.jiangnan.entity.UserBorrowBookInfo
import com.jiangnan.repository.AdminRepository
import kotlinx.coroutines.launch

class AdminViewModel : ViewModel() {

    val adminRepository = AdminRepository()

    /**
     * 删除图书的返回信息
     */
    private val _deleteBookResponse = MutableLiveData<Res<Any>>()
    val deleteBookResponse: LiveData<Res<Any>> = _deleteBookResponse
    /**
     * 修改图书的返回信息
     */
    private val _updateBookResponse = MutableLiveData<Res<Any>>()
    val updateBookResponse: LiveData<Res<Any>> = _updateBookResponse

    /**
     * 查询所有用户的图书借阅情况
     */
    private val _userInfoPageResult = MutableLiveData<Res<PageResult<UserBorrowBookInfo>>>()
    val userInfoPageResult: LiveData<Res<PageResult<UserBorrowBookInfo>>> = _userInfoPageResult

    /**
     * 修改用户状态
     */
    private val _updateUserStatusResponse = MutableLiveData<Res<Any>>()
    val updateUserStatusResponse: LiveData<Res<Any>> = _updateUserStatusResponse

    private val _toastMessage = MutableLiveData<String>()
    val toastMessage : LiveData<String> = _toastMessage

    /**
     * 根据id删除图书
     */
    fun deleteBookById(id: Int) {
        viewModelScope.launch {
            try {
                val response = adminRepository.deleteBookById(id)
                if(response.code == 1){
                    _deleteBookResponse.postValue(response)
                    _toastMessage.postValue("删除成功")
                }else{
                    _toastMessage.postValue("删除失败")
                }
            } catch (e: Exception) {
                e.printStackTrace()
                _toastMessage.postValue("删除失败")
            }
        }
    }
    /**
     * 修改图书
     */
    fun updateBook(book: com.jiangnan.entity.Book) {
        viewModelScope.launch {
            try {
                val response = adminRepository.updateBook(book)
                if (response.code == 1){
                    _updateBookResponse.postValue(response)
                    _toastMessage.postValue("修改成功")
                }else{
                    _toastMessage.postValue("修改失败")
                }
            } catch (e: Exception) {
                e.printStackTrace()
                _toastMessage.postValue("修改失败")
            }
        }
    }
    /**
     * 查询所有用户的图书借阅情况
     */
    fun getUserInfoList(page: Int, pageSize: Int,requestParam: String) {
        viewModelScope.launch {
            try {
                val response = adminRepository.getUserInfoList(page,pageSize,requestParam)
                if (response.code == 1){
                    var total = response.data?.total
                    _toastMessage.postValue("查询成功 ${total}")
                    if (page == 1){
                        _userInfoPageResult.postValue(response)
                    }else{
                        var currentList = _userInfoPageResult.value?.data?.rows
                        response.data?.rows?.let {
                            currentList?.addAll(it)
                            _userInfoPageResult.postValue(Res(1,"查询成功",PageResult(total!!,currentList)))
                        }
                    }

                }else{
                    _toastMessage.postValue("查询失败")
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }

    /**
     * 修改用户状态
     */
    fun updateUserStatus(id: Int, status: Int) {
        viewModelScope.launch {
            try {
                val response = adminRepository.updateUserStatus(id, status)
                if (response.code == 1){
                    _updateUserStatusResponse.postValue(response)
                    _toastMessage.postValue("修改成功")
                }else{
                    _toastMessage.postValue("修改失败")
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }
}